//
//  YMFOrderViewController.swift
//  FlyingPigeon
//
//  Created by yang on 2019/5/22.
//  Copyright © 2019年 a. All rights reserved.
//

import Foundation
import UIKit


class YMFOrderCell: UITableViewCell {
    @IBOutlet weak var orderCode: UILabel!
    @IBOutlet weak var carImage: UIImageView!
    @IBOutlet weak var carName: UILabel!
    @IBOutlet weak var timeInfo: UILabel!
    @IBOutlet weak var totalPrice: UILabel!
    
    
    
    
    override func layoutSubviews() {
        super.layoutSubviews()
        
        contentView.frame = CGRect(x: 10, y: 20, width: self.bounds.width - 20, height: self.bounds.height - 40)
        
    }
    
    
    func updateCellWith(model: YMFOrderModel) {
        orderCode.text = "订单编号：" + model.orderCode
        carImage.image = UIImage(named: model.carImage)
        carName.text = model.carName
        
        let formatt = DateFormatter()
        formatt.dateFormat = "YYYY年MM月dd日"
        var begainStr = ""
        if let begainDate = model.begainDate {
            begainStr = formatt.string(from: begainDate)
        }
        var endStr = ""
        if let endDate = model.endDate {
            endStr = formatt.string(from: endDate)
        }
        timeInfo.text = begainStr + "-" + endStr
        
        let priceStr = "订单金额：￥" + model.totalPrice
        let attrStr = NSMutableAttributedString(string: priceStr)
        attrStr.addAttributes([NSAttributedStringKey.foregroundColor : UIColor.red], range: NSRange(location: 6, length: priceStr.count - 6))
        totalPrice.attributedText = attrStr
    }
    
}

class YMFOrderViewController: UIViewController {
    var dateArray = [YMFOrderModel]()
    
    let tableView = UITableView()
    let infoLabel = UILabel()
    
    override func viewDidLoad() {
        super.viewDidLoad()
        view.backgroundColor = UIColor.white
        
        navigationItem.title = "订单"
        tableView.dataSource = self
        tableView.delegate = self
        tableView.register(UINib(nibName: "YMFOrderCell", bundle: nil), forCellReuseIdentifier: "cell")
        tableView.backgroundColor = UIColor(red: 245 / 255.0, green: 245 / 255.0, blue: 245 / 255.0, alpha: 1)
        tableView.frame = CGRect(x: 0, y: 0, width: view.bounds.width, height: view.bounds.height - 64)
        view.addSubview(tableView)
        
       
        infoLabel.text = "暂无订单信息"
        infoLabel.textAlignment = .center
        infoLabel.textColor = UIColor(red: 100 / 255.0, green: 100 / 255.0, blue: 100 / 255.0, alpha: 1)
        infoLabel.isHidden = true
        view.addSubview(infoLabel)
        infoLabel.sizeToFit()
        infoLabel.center = CGPoint(x: view.bounds.size.width * 0.5, y: view.bounds.size.height * 0.5)
        
    }
    
    override func viewWillAppear(_ animated: Bool) {
        super.viewWillAppear(animated)
        
        dateArray = YMFOrderInfoManager.share().getData()
        tableView.reloadData()
        infoLabel.isHidden = dateArray.count != 0
    }
    
   
}


extension YMFOrderViewController:UITableViewDataSource, UITableViewDelegate  {
    func numberOfSections(in tableView: UITableView) -> Int {
        return 1
    }
    
    func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
        return dateArray.count
    }
    
    func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
        let cell = tableView.dequeueReusableCell(withIdentifier: "cell") as? YMFOrderCell
        cell?.backgroundColor = UIColor(red: 245 / 255.0, green: 245 / 255.0, blue: 245 / 255.0, alpha: 1)
        cell?.contentView.backgroundColor = UIColor.white
        cell?.contentView.layer.cornerRadius = 10
        cell?.contentView.clipsToBounds = true
        
        cell?.updateCellWith(model: dateArray[indexPath.row])
        return cell!
    }
    
    func tableView(_ tableView: UITableView, heightForRowAt indexPath: IndexPath) -> CGFloat {
        return 220
    }
    
    func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
        tableView.deselectRow(at: indexPath, animated: true)
        
        let vc = YMFEditOrderViewController()
        vc.orderModel = dateArray[indexPath.row]
        navigationController?.pushViewController(vc, animated: true)
    }
}
